﻿using Commonality.DAL.MySQL;
using Commonality.Model;
using Commonality.Model.Tray;
using Merge.BLL;
using Merge.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WZS_Auto_Classifying_Merge
{
    public partial class ConfigForm : Form
    {
        public ConfigForm()
        {
            InitializeComponent();
        }

        private void ConfigForm__Load(object sender, EventArgs e)
        {
            Tray.TrayInit();
            MySqlFunction.InitMySQLFunction();
            Parameter.MySQLParameterLoad();
            Parameter.TrayModelElementLoad();
            Parameter.TrayGatherParameterLoad();
            Parameter.PackagingOrientation.PackagingOrientationInit();
            Parameter.OrientationGotoPackagingParameterLoad();
            MySQLParameterDisplay();
            TrayModelElementDisplay();
            TrayMapDisplay();
            checkBoxPackagingOrientationInitDisplay();
            OrientationGotoPackaging_ComboBoxDisplay();
        }
        private void MySQLParameterDisplay()
        {
            MySQLHost.Text = MySqlFunction.MySql.DataSource;
            MySQLPassword.Text = MySqlFunction.MySql.Password;
            UserId.Text = MySqlFunction.MySql.UserId;
        }

        private void MySQLHost_TextChanged(object sender, EventArgs e)
        {
            string host = (sender as TextBox).Text;
            Parameter.UpdateMySQLHost(host);
            Log4Net.MsgInfo("修改MySQL主机为：" + host);
        }

        private void MySQLPassword_TextChanged(object sender, EventArgs e)
        {
            string password = (sender as TextBox).Text;
            Parameter.UpdateMySQLPassword(password);
            Log4Net.MsgInfo("修改MySQL连接密码：" + password);
        }

        private void UserId_TextChanged(object sender, EventArgs e)
        {
            string userId = (sender as TextBox).Text;
            Parameter.UpdateMySQLUserId(userId);
            Log4Net.MsgInfo("UserID：" + userId);
        }
        private void TrayModelElementDisplay()
        {
            foreach (string item in Tray.X01)
            {
                X01TextBox.Text += (item + "\r\n");
            }
            foreach (string item in Tray.X03)
            {
                X03TextBox.Text += (item + "\r\n");
            }
            foreach (string item in Tray.X05)
            {
                X05TextBox.Text += (item + "\r\n");
            }
            foreach (string item in Tray.X08)
            {
                X08TextBox.Text += (item + "\r\n");
            }
            foreach (string item in Tray.X13)
            {
                X13TextBox.Text += (item + "\r\n");
            }
            foreach (string item in Tray.X15)
            {
                X15TextBox.Text += (item + "\r\n");
            }

            foreach (var item in Tray.X176)
            {
                X176TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X177)
            {
                X177TextBox.Text += (item + "\r\n");
            }
            foreach (var item in Tray.X178)
            {
                X178TextBox.Text += (item + "\r\n");
            }

            foreach (var item in Tray.X19)
            {
                X19TextBox.Text += (item + "\r\n");
            }
        }

        private void UpdateTrayModelElement_TextBox(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }

            switch ((sender as TextBox).Name)
            {
                case "X01TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X01);
                    Parameter.UpdateTrayModelElement(TrayModel.X01, Tray.X01);
                    break;
                case "X03TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X03);
                    Parameter.UpdateTrayModelElement(TrayModel.X03, Tray.X03);
                    break;
                case "X05TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X05);
                    Parameter.UpdateTrayModelElement(TrayModel.X05, Tray.X05);
                    break;
                case "X08TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X08);
                    Parameter.UpdateTrayModelElement(TrayModel.X08, Tray.X08);
                    break;
                case "X13TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X13);
                    Parameter.UpdateTrayModelElement(TrayModel.X13, Tray.X13);
                    break;
                case "X15TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X15);
                    Parameter.UpdateTrayModelElement(TrayModel.X15, Tray.X15);
                    break;
                case "X176TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X176);
                    Parameter.UpdateTrayModelElement(TrayModel.X176, Tray.X176);
                    break;
                case "X177TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X177);
                    Parameter.UpdateTrayModelElement(TrayModel.X177, Tray.X177);
                    break;
                case "X178TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X178);
                    Parameter.UpdateTrayModelElement(TrayModel.X178, Tray.X178);
                    break;
                case "X19TextBox":
                    GetModelSetsInTextBox(sender, ref Tray.X19);
                    Parameter.UpdateTrayModelElement(TrayModel.X19, Tray.X19);
                    break;
                default:
                    break;
            }
        }
        private void GetModelSetsInTextBox(object sender, ref List<string> model)
        {
            model.Clear();
            string[] data = (sender as TextBox).Lines;
            foreach (string item in data)
            {
                string[] str = Regex.Split(item, "\\s+", RegexOptions.IgnoreCase);
                foreach (string item_ in str)
                {
                    if (item_ != "" && item_ != null)
                    {
                        model.Add(item_);
                    }
                }
            }
            ListDeWeight(model, ref model);
        }
        /// <summary>
        /// 移除重复元素
        /// </summary>
        /// <param name="inp"></param>
        /// <param name="outp"></param>
        private void ListDeWeight(List<string> inp, ref List<string> outp)
        {
            HashSet<string> data = new HashSet<string>(inp);
            outp.Clear();
            foreach (string item in data)
            {
                outp.Add(item);
            }
        }
        private void Seat_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch ((sender as ComboBox).Name)
            {
                case "SeatX01":
                    Parameter.TrayGatherParameterMap(1, (Seat)SeatX01.SelectedIndex);
                    break;
                case "SeatX03":
                    Parameter.TrayGatherParameterMap(2, (Seat)SeatX03.SelectedIndex);
                    break;
                case "SeatX05":
                    Parameter.TrayGatherParameterMap(3, (Seat)SeatX05.SelectedIndex);
                    break;
                case "SeatX08":
                    Parameter.TrayGatherParameterMap(4, (Seat)SeatX08.SelectedIndex);
                    break;
                case "SeatX13":
                    Parameter.TrayGatherParameterMap(5, (Seat)SeatX13.SelectedIndex);
                    break;
                case "SeatX15":
                    Parameter.TrayGatherParameterMap(6, (Seat)SeatX15.SelectedIndex);
                    break;
                case "SeatX176":
                    Parameter.TrayGatherParameterMap(7, (Seat)SeatX176.SelectedIndex);
                    break;
                case "SeatX177":
                    Parameter.TrayGatherParameterMap(8, (Seat)SeatX177.SelectedIndex);
                    break;
                case "SeatX178":
                    Parameter.TrayGatherParameterMap(9, (Seat)SeatX178.SelectedIndex);
                    break;
                case "SeatX19":
                    Parameter.TrayGatherParameterMap(10, (Seat)SeatX19.SelectedIndex);
                    break;
                default:
                    break;
            }

        }

        /// <summary>
        /// 显示空Tray映射关系
        /// </summary>
        private void TrayMapDisplay()
        {
            SeatX01.SelectedIndex = (int)TrayMap.X01;
            SeatX03.SelectedIndex = (int)TrayMap.X03;
            SeatX05.SelectedIndex = (int)TrayMap.X05;
            SeatX08.SelectedIndex = (int)TrayMap.X08;
            SeatX13.SelectedIndex = (int)TrayMap.X13;
            SeatX15.SelectedIndex = (int)TrayMap.X15;
            SeatX176.SelectedIndex = (int)TrayMap.X176;
            SeatX177.SelectedIndex = (int)TrayMap.X177;
            SeatX178.SelectedIndex = (int)TrayMap.X178;
            SeatX19.SelectedIndex = (int)TrayMap.X19;
        }

        private void checkBoxPackagingOrientation_CheckedChanged(object sender, EventArgs e)
        {
            //Parameter.PackagingOrientation.X13Unilateral = checkBoxPackagingOrientation.Checked;
        }
        private void checkBoxPackagingOrientationInitDisplay()
        {
            //Parameter.PackagingOrientation.X13Unilateral = true;
            //checkBoxPackagingOrientation.Checked = true;
        }
        private void OrientationGotoPackaging_ComboBoxDisplay()
        {
            X1OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X01;
            X3OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X03;
            X5OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X05;
            X8OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X08;
            X13OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X13;
            X15OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X15;
            X176OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X176;
            X177OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X177;
            X178OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X178;
            X19OrientationGotoPackaging_ComboBox.SelectedIndex = TrayOrientationGotoPackaging.X19;
        }
        private void OrientationGotoPackaging_ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch ((sender as ComboBox).Name)
            {
                case "X1OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X01 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X01, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X3OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X03 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X03, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X5OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X05 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X05, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X8OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X08 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X08, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X13OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X13 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X13, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X15OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X15 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X15, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X176OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X176 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X176, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X177OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X177 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X177, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X178OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X178 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X178, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                case "X19OrientationGotoPackaging_ComboBox":
                    TrayOrientationGotoPackaging.X19 = (short)(sender as ComboBox).SelectedIndex;
                    Parameter.OrientationGotoPackagingParameterSet(TrayModel.X19, (TrayOrientationGotoPackaging.Ori)(sender as ComboBox).SelectedIndex);
                    break;
                default:
                    break;
            }
        }
    }
}
